import { execute } from "../../database/connector";

import { ResearchGroupQueries } from "../queries/research-group.query";
import { IResearchGroup } from "../../models/research-group.model";

/**
 * gets active researchGroup
 */
export const getResearchGroups = async () => {
  return execute<IResearchGroup[]>(ResearchGroupQueries.GetResearchGroups, []);
};

/**
 * gets a researchGroup based on id provided
 */
export const getResearchGroupById = async (id: IResearchGroup['id']) => {
  return execute<IResearchGroup[]>(ResearchGroupQueries.GetResearchGroupById, [id]);
};

/**
 * gets a researchGroup based on id provided
 */
export const getResearchGroupByOwnerId = async (id: IResearchGroup['id']) => {
  return execute<IResearchGroup[]>(ResearchGroupQueries.GetResearchGroupByOwnerId, [id]);
};

/**
 * gets a researchGroup based on id provided
 */
export const getResearchGroupByName = async (researchGroupName: IResearchGroup['researchGroupName']) => {
  return execute<IResearchGroup[]>(ResearchGroupQueries.GetResearchGroupByName, [researchGroupName]);
};

/**
 * adds a new active researchGroup record
 */
export const insertResearchGroup = async (researchGroup: IResearchGroup) => {
  const result = await execute<{ affectedRows: number }>(ResearchGroupQueries.AddResearchGroup, [
    [Object.values(researchGroup)]
  ]);
  return result.affectedRows > 0;
};

/**
 * updates researchGroup information based on the id provided
 */
export const updateResearchGroup = async (researchGroup: IResearchGroup) => {
  const result = await execute<{ affectedRows: number }>(ResearchGroupQueries.UpdateResearchGroupById, [
    researchGroup,
    researchGroup.id
  ]);
  return result.affectedRows > 0;
};

/**
 * updates researchGroup information based on the id provided
 */
 export const deleteResearchGroup = async (id: IResearchGroup['id']) => {
  const result = await execute<{ affectedRows: number }>(ResearchGroupQueries.DeleteResearchGroupById, [
    id
  ]);
  return result.affectedRows > 0;
};